package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @author zcf
 */
@Component
public class RedisIdWorker {
  /*  第一部分：符号位：1bit，永远为0

    时间戳：31bit，以秒为单位，可以使用69年

    序列号：32bit，秒内的计数器，支持每秒产生2^32个不同ID*/
    /**
     * 开始时间戳
     */
    private static final long BEGIN_TIMESTAMP = 1640995200L;
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 序列号的位数
     */
    private static final int COUNT_BITS = 32;


    public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public long nextID(String keyPrefix) {
        //1.生成时间戳
        LocalDateTime now = LocalDateTime.now();
        long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
        long timestamp = nowSecond - BEGIN_TIMESTAMP;

        //2.生成序列号
        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        //生成的日期如：2024:05:21
        //2.1自增长
        Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);

        return timestamp <<COUNT_BITS | count;
    }


}
